<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="文章标题" prop="blogId">
          <el-select v-model="queryParams.blogId" placeholder="请选择文章" clearable>
            <el-option
              v-for="(item, index) in blogOptions"
              :key="index"
              :value="item.id"
              :label="item.blogTitle"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="userId">
          <el-select v-model="queryParams.userId" placeholder="请选择用户" clearable>
            <el-option
              v-for="(item, index) in userOptions"
              :key="index"
              :value="item.id"
              :label="item.userName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="ip地址" prop="ipAddress">
          <el-input
            v-model="queryParams.ipAddress"
            placeholder="请输入ip地址"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['blog:viewRecord:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['blog:viewRecord:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <vxe-table
        :loading="loading"
        resizable
        row-key
        ref="tableRef"
        :row-config="{isHover: true}"
        :data="viewRecordList"
        @checkbox-all="handleSelectionChange"
        @checkbox-change="handleSelectionChange"
        :class="showSearch ? 'table-container' : 'table-container-full'"
      >
        <vxe-column type="checkbox" align="center" min-width="60"/>
        <vxe-column title="主键" field="id" align="center" min-width="100"/>
        <vxe-column title="文章标题" field="blogId" align="center" min-width="150"/>
        <vxe-column title="用户名" field="userId" align="center" min-width="150"/>
        <vxe-column title="ip地址" field="ipAddress" align="center" min-width="150"/>
        <vxe-column title="浏览器信息" field="browserInfo" align="center" min-width="150"/>
        <vxe-column title="操作系统信息" field="osInfo" align="center" min-width="150"/>
        <vxe-column title="备注" field="remark" align="center" min-width="150"/>
        <vxe-column title="操作" align="center" fixed="right" min-width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['blog:viewRecord:remove']"
            >删除
            </el-button>
          </template>
        </vxe-column>
      </vxe-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import {listViewRecord, getViewRecord, delViewRecord, queryUserSelect, queryBlogSelect} from "@/api/blog/viewRecord";

export default {
  name: "ViewRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章浏览记录表格数据
      viewRecordList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        blogId: null,
        userId: null,
        ipAddress: null,
      },
      // 用户下拉数据
      userOptions: [],
      // 文章下拉数据
      blogOptions: []
    };
  },
  created() {
    this.getUserList();
    this.getBlogList();
    this.getList();
  },
  methods: {
    /** 查询文章浏览记录列表 */
    getList() {
      this.loading = true;
      listViewRecord(this.queryParams).then(response => {
        this.viewRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(row) {
      this.ids = row.records.map(item => item.id)
      this.single = row.records.length !== 1
      this.multiple = !row.records.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文章浏览记录编号为"' + ids + '"的数据项？').then(function () {
        return delViewRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blog/viewRecord/export', {
        ...this.queryParams
      }, `viewRecord_${new Date().getTime()}.xlsx`)
    },
    /** 获取用户列表 */
    getUserList() {
      queryUserSelect([]).then(res => {
        this.userOptions = res.data;
      })
    },
    /** 获取文章列表 */
    getBlogList() {
      queryBlogSelect({}).then(res => {
        this.blogOptions = res.data;
      })
    }
  }
};
</script>
